﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using Lizk.Utilities;

namespace Lizk.Media
{


    public class ImageCache : FileCache
    {
        public string Root { get; set; }
        public Size MaxImageSize { get; set; }

        public ImageCache(string root)
        {
            this.Root = root;
            MaxImageSize = Size.Empty;
        }

        private string MakeCachePath(TestData.TitlesRow title) { return Path.ChangeExtension(Path.Combine(Root, title.Type + Path.DirectorySeparatorChar + title.Name), ".png"); }

        public override bool Contains(TestData.TitlesRow title)
        {
            string path = MakeCachePath(title);
            return File.Exists(path);
        }

        public string TryGetImagePath(TestData.TitlesRow title, string def)
        {
            string path = Contains(title) ? MakeCachePath(title) : null;
            return path != null ? path : def;
        }

        public override bool Save(TestData.TitlesRow title, string artworkPath)
        {
            string cachePath = MakeCachePath(title);

            if (!Directory.Exists(Path.GetDirectoryName(cachePath)))
                Directory.CreateDirectory(Path.GetDirectoryName(cachePath));

            try
            {
                using (Image image = Image.FromFile(artworkPath))
                {
                    if (MaxImageSize != Size.Empty)
                    {
                        using (Image g = ImageTools.Resize(image, MaxImageSize, true, System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic))
                            g.Save(cachePath, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else
                        image.Save(cachePath, System.Drawing.Imaging.ImageFormat.Png);
                }
            }
            catch
            {
                return false;
            }
            return true;
        }

        public bool Save(TestData.TitlesRow title, Image image)
        {
            string cachePath = MakeCachePath(title);

            if (!Directory.Exists(Path.GetDirectoryName(cachePath)))
                Directory.CreateDirectory(Path.GetDirectoryName(cachePath));

            try
            {
                if (MaxImageSize != Size.Empty)
                {
                    using (Image g = ImageTools.Resize(image, MaxImageSize, true, System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic))
                        g.Save(cachePath, System.Drawing.Imaging.ImageFormat.Png);
                }
                else
                    image.Save(cachePath, System.Drawing.Imaging.ImageFormat.Png);
            }
            catch
            {
                return false;
            }
            return true;
        }



        public override string FindExisting(string titlePath)
        {
            return MediaUtil.FindImage(titlePath, MediaUtil.BasicPictureExtensions);
        }


        public override void Distribute(TestData.TitlesRow title, string showPath)
        {
            if (Contains(title))
            {
                try
                {
                    string path = MakeCachePath(title);
                    File.Copy(path, Path.Combine(showPath, Path.GetFileName(path)), true);
                }
                catch { }
            }
        }
    }
}
